import Mock from 'mockjs'
const Random = Mock.Random

// 根据url获取query参数
const getQuery = (url,name)=> {
  const index = url.indexOf('?')
  if(index !== -1){
      const queryStrArr = url.substr(index+1).split('&')
      for(let i = 0 ; i < queryStrArr.length ; i++){
          const itemArr = queryStrArr[i].split('=')
          if(itemArr[0] === name){
              return itemArr[1]
          }
      }
  }
  return null
  
}

const { citys } = Mock.mock({
  'citys|10': [{
    'id|+1': 1,
    city: () => { return Random.city() }, //return 返回数据
    color: () => { return Random.color() },
    'type_id|1': [1, 2, 3, 4]
  }]
});

// 创建city数据，请求全部
// Mock.mock('http://localhost:8081/api/city', 'get', () => {
//   return { cityList: citys,code:200 }
// })
Mock.mock(/\/api\/city/, 'get', () => {
  return { cityList: citys,code:200 }
})

//创建city数据，传参请求
Mock.mock(/\/api\/city\/find/, 'post', (data) => {
  // console.log(data);
  const body =JSON.parse(data.body)
  let result=citys.filter(item=>{
    return item.type_id==body.type_id;
  })
  return{
    result,
    code:200
  }
})